package com.liao.examine;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private ListView listView;

    private String s = "top";
    private String url = "http://v.juhe.cn/toutiao/index?type=" + s + "&key=2cd4fb6e6cc06b24142ff03863a7edb4";

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            adapter.notifyDataSetChanged();
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getData();
    }

    private ItemListviewLayoutAdapter adapter;
    private ArrayList<NewsInfo> newsInfos;

    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
        newsInfos = new ArrayList<>();
        adapter = new ItemListviewLayoutAdapter(MainActivity.this, newsInfos);
        listView.setAdapter(adapter);
    }

    private void getData() {
        new Thread() {
            @Override
            public void run() {
                String data = HttpUtil.getData(url);
                json(data);
            }
        }.start();
    }


    private void json(String data) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(data);
            JSONObject result = jsonObject.getJSONObject("result");
            JSONArray jsonArray = result.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                NewsInfo newsInfo = new NewsInfo();
                newsInfo.setTitle(object.getString("title"));
                newsInfo.setDate(object.getString("date"));
                String thumbnail_pic_s = null;
                thumbnail_pic_s = object.getString("thumbnail_pic_s");
                try {
                    String thumbnail_pic_s1 = object.getString("thumbnail_pic_s02");
                    try {
                        String thumbnail_pic_s2 = object.getString("thumbnail_pic_s03");
                        System.out.println("MainActivity.json=======================" + thumbnail_pic_s1);
                        newsInfo.setThumbnail_pic_s(thumbnail_pic_s);
                        newsInfo.setThumbnail_pic_s02(thumbnail_pic_s1);
                        newsInfo.setThumbnail_pic_s03(thumbnail_pic_s2);
                        newsInfo.setAuthor_name(object.getString("author_name"));
                        newsInfos.add(newsInfo);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            handler.sendEmptyMessage(0x001);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
